import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import type { AsyncThunk, ActionReducerMapBuilder, PayloadAction, Slice } from "@reduxjs/toolkit";
import { getConfig } from "./appAPI";
import type { GetConfigParams } from './appAPI'

type UserInfo = {
  admin_user_id: number,
  user_token: string,
  userName: string
}

interface InitialState {
  isShowMenu: boolean,
  userInfo: UserInfo | null,
  config: {
    status: 'init' | 'loading' | 'success' | 'error',
    data: Res | undefined
  }
}

const initialState: InitialState = {
  isShowMenu: false,
  userInfo: null,
  config: {
    status: 'init',
    data: undefined
  }
};

type Res = {
  data: {
    admin_permission: { label: string, value: number }[],
    switch_conf: { label: string, value: number }[],
    user_channel_list: { label: string, value: number }[],
    wd_country_conf: { label: string, value: string }[],
    wd_review_whitelist: { label: string, value: string }[],
    wd_way_conf: { label: string, value: number }[],
    wd_way_status: { label: string, value: number }[],
    rank_type_map: { label: string, value: number }[],
    version_path_type: { label: string, value: number }[],
    announce_type_map: { label: string, value: number }[],
    announce_stat_map: { label: string, value: number }[],
    rank_gear_map: { label: string, value: number }[],
    wallet_source_map: { label: string, value: number }[],
    user_network_list: { label: string, value: number }[],
    backend_modify_type: { label: string, value: number }[],
    run_stat_type_map: { label: string, value: number }[],
    minigame_class: { label: string, value: number }[],
    wd_order_status: { label: string, value: number }[],
    user_export_conf: { label: string, value: number }[],
    time_export_conf: { label: string, value: number }[]
  }
}
export const getConfigAsync: AsyncThunk<Res, GetConfigParams, {}> = createAsyncThunk<Res, GetConfigParams, {}>('app/getConfigAsync', async (params) => {
  const res: Res = await getConfig(params)
  return res
})
const configStatus: (builder: ActionReducerMapBuilder<InitialState>) => void = (builder: ActionReducerMapBuilder<InitialState>): void => {
  builder.addCase(getConfigAsync.pending, (state: InitialState): void => {
    state.config.status = 'loading'
  }).addCase(getConfigAsync.fulfilled, (state: InitialState, action: PayloadAction<Res>): void => {
    state.config.status = 'success'
    state.config.data = action.payload
  }).addCase(getConfigAsync.rejected, (state: InitialState): void => {
    state.config.status = 'error'
  })
}

type Reducers = {
  changeIsShowMenu: (state: InitialState, action: PayloadAction<boolean>) => void,
  setUserInfo: (state: InitialState, action: PayloadAction<UserInfo>) => void
}
type Selectors = {}
const appSlice: Slice<InitialState, Reducers, 'app', 'app', Selectors> = createSlice<InitialState, Reducers, 'app', Selectors, 'app'>({
  name: "app",
  initialState,
  reducers: {
    changeIsShowMenu(state: InitialState, action: PayloadAction<boolean>): void {
      state.isShowMenu = action.payload
    },
    setUserInfo(state: InitialState, action: PayloadAction<UserInfo>): void {
      state.userInfo = action.payload
    }
  },
  extraReducers(builder: ActionReducerMapBuilder<InitialState>): void {
    configStatus(builder)
  },
});

export const { changeIsShowMenu, setUserInfo }: typeof appSlice.actions = appSlice.actions;

export default appSlice.reducer;
